package com.mazaiting.email.service.impl;

import com.mazaiting.email.domain.info.MailDetail;
import com.mazaiting.email.service.IMailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.List;

/**
 * 邮件发送服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MailServiceImpl implements IMailService {

    private final JavaMailSender javaMailSender;

    @Override
    public boolean send(MailDetail mailDetail) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            // 发件人地址
            InternetAddress fromAddress = new InternetAddress(MimeUtility.encodeText(mailDetail.getFromMail()) + "<" + mailDetail.getFromMail() + ">");
            messageHelper.setFrom(fromAddress);
            // 收件人地址
            InternetAddress toAddress = new InternetAddress(MimeUtility.encodeText(mailDetail.getToMail()) + "<" + mailDetail.getToMail() + ">");
            messageHelper.setTo(toAddress);
            // 邮件名称
            messageHelper.setSubject(mailDetail.getTitle());
            // 第二个参数指定发送的是HTML格式
            messageHelper.setText(mailDetail.getContent(), mailDetail.getHtml());
            // 抄送人
            if (mailDetail.getCc() != null && mailDetail.getCc().length > 0) {
                messageHelper.setCc(mailDetail.getCc());
            }
            // 附件不为空
            List<File> attachments = mailDetail.getAttachments();
            if (!CollectionUtils.isEmpty(attachments)) {
                for (File file : attachments) {
                    messageHelper.addAttachment(file.getName(), file);
                }
            }
            javaMailSender.send(message);
            return true;
        } catch (Exception e) {
            log.error("mailServiceImpl.sendMessage.error:{},{}", e.getMessage(), e.getStackTrace());
        }
        return false;
    }
//    /**
//     * 谁发送的
//     */
//    @Value("${spring.mail.username:1072067273@qq.com}")
//    private String from;
//
//    /**
//     * 邮件发送服务
//     */
//    @Resource
//    private JavaMailSender javaMailSender;
//
//    @Override
//    public boolean send(MailVO mailVO){
//        try {
//            SimpleMailMessage mailMessage = new SimpleMailMessage();
//            mailMessage.setFrom(from);
//            mailMessage.setTo(mailVO.getTo());
//            mailMessage.setSubject(mailVO.getSubject());
//            mailMessage.setText(mailVO.getContent());
//            javaMailSender.send(mailMessage);
//            return true;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    @Override
//    public boolean send(String to, String code) {
//        MimeMessage mimeMessage = null;
//        try {
//            mimeMessage = javaMailSender.createMimeMessage();
//            mimeMessage.setFrom(from);
//            mimeMessage.setRecipients(Message.RecipientType.TO,to);
//            mimeMessage.setSubject("APOLLO | 安全操作验证码");
//            String message = "您在 "+ DateTimeUtil.time2String(LocalDateTime.now()) +" 通过APOLLO平台，您本次操作的验证码是 "+code+"，有效期10分钟。\n" +
//                    "\r\n" +
//                    "[本邮件由系统自动发送，请勿直接回复]";
//            mimeMessage.setText(message,"UTF-8");
//            javaMailSender.send(mimeMessage);
//            return true;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    @Override
//    public boolean sendHtmlMail(MailVO mailVO) {
//        MimeMessage mimeMessage = null;
//        MimeMessageHelper messageHandler = null;
//        try {
//            mimeMessage = javaMailSender.createMimeMessage();
//            // true 表示 该消息为multipart(多部分数据传输) Message
//            messageHandler = new MimeMessageHelper(mimeMessage,true);
//            // 设置发送者，个人昵称
//            messageHandler.setFrom(from,mailVO.getPersonal());
//            // 设置接收者
//            messageHandler.setTo(mailVO.getTo());
//            // 设置主题
//            messageHandler.setSubject(mailVO.getSubject());
//            // 设置内容
//            messageHandler.setText(mailVO.getContent(),true);
//            javaMailSender.send(mimeMessage);
//            return true;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return false;
//    }


//    public void send(SimpleMailMessage[] simpleMessages) throws MailException {
//        List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(simpleMessages.length);
//        for (SimpleMailMessage simpleMessage : simpleMessages) {
//            MimeMailMessage message = new MimeMailMessage(createMimeMessage());
//            simpleMessage.copyTo(message);
//            mimeMessages.add(message.getMimeMessage());
//        }
//        doSend(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), simpleMessages);
//    }
//
//
//
//    public MimeMessage createMimeMessage(){
//        return new MimeMessage();
//    }
//
//    private void doSend(MimeMessage[] toArray, SimpleMailMessage[] simpleMessages) {
//    }


}
